from rich.console import Console
from urllib import request
import json
console = Console()  # 创建控制台

orange = '#ffb74d'
cyan = '#63e6be'
grey = '#bdbdbd'

# 用requests库的get()方法简明地获取网页信息。第一个函数返回一个网页内容---r.text.


def GetData():
    url = "https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions?locale=zh-CN&country=HK&allowCountries=HK,CN"  # 请求的url
    headers = {  # 请求的头部
        "origin": "https://www.epicgames.com",
        "referer": "https://www.epicgames.com/",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63",
        "x-requested-with": "XMLHttpRequest"
    }

    req = request.Request(url=url, headers=headers)  # GET无data项
    res = request.urlopen(req)
    res = str(res.read(), encoding='utf-8')  # 将返回的bytes类型转为str类型
    data = json.loads(res)  # 如果是json/dict类型,这一步可以转为dict类型,前提是从str转
#    console.log("[bold green]请求数据成功√：%s" % url)
#    console.print(data)
    # 返回免费游戏的列表（每个游戏都存于一个字典中）
    console.log('[bold green]已获取Epic免费游戏列表√')
    return data.get('data').get('Catalog').get('searchStore').get('elements')
